package biz

import (
	"org.knight/api"
	"org.knight/data"
)

type SimplePipelineDiscriminator struct {
}

func (d SimplePipelineDiscriminator) ChoosePipeline(e api.Event) string {
	//log.Printf("try choose pipeline")
	return "ie"
}

type SimplePipeline struct {
	Handlers   []api.Handler
	SpecHandlers []api.SpecHandler
	ReadyQueue chan data.InfoElement
	CtxCache   map[string]*api.Context
}

func (s SimplePipeline) OnEvent(e api.Event) {

	//log.Printf("trying process event : %s", e)

	ctx := s.CtxCache[e.TID]
	newTx := false
	if ctx == nil {
		ctx = &api.Context{ReadyQueue: s.ReadyQueue}
		s.CtxCache[e.TID] = ctx
		newTx = true
	}
	ctx.E = &e
	ctx.SpecName = e.SpecName
	ctx.SpecVersion = e.SpecVersion

	if newTx {
		for _, handler := range s.SpecHandlers {
			handler.HandleStart(ctx)
		}
	}

	// log.Printf("processing ctx %s", ctx)
	for _, handler := range s.Handlers {
		handler.Handle(ctx)
	}

}
